package com.csu.letaobackend.controllerM;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.csu.letaobackend.bean.*;
import com.csu.letaobackend.commom.CommonResponse;
import com.csu.letaobackend.dto.ItemVO2;
import com.csu.letaobackend.serviceM.CatalogMService;
import com.csu.letaobackend.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("/admin/catalog")
public class CatalogMController {
      @Autowired
      CatalogMService catalogMService;

      @GetMapping("/categories")
      @ResponseBody
      public CommonResponse<List<CategoryVO>> getCategories() {

            return catalogMService.getCategories();
      }

      @GetMapping("/categories/{id}")
      @ResponseBody
      public CommonResponse<List<ProductVO>> getProductsByCategory(@PathVariable int id) {
            return catalogMService.getProductsByCategoryId(id);
      }

      @GetMapping("/categories/products/{id}")
      @ResponseBody
      public CommonResponse<List<ItemVO>> getItemsByProduct(@PathVariable int id, @RequestParam int sortby, HttpSession httpSession) {

            return catalogMService.getItemsByProductId(id, sortby, null);
      }

      @GetMapping("/categories/products/{id}/pages/{pagesize}/{pageid}")
      @ResponseBody
      public CommonResponse<IPage<ItemVO>> getItemsPageByProduct(@PathVariable int id, @RequestParam int sortby, @PathVariable int pagesize, @PathVariable int pageid, HttpSession httpSession) {

            return catalogMService.getItemsPages(id, sortby, pageid, pagesize, null);
      }


      @GetMapping("/items/pages/{pagesize}/{pageid}")
      @ResponseBody
      CommonResponse<IPage<ItemVO>> getAllItems(@RequestParam int sortby, @PathVariable int pagesize, @PathVariable int pageid, HttpSession httpSession) {

            return catalogMService.getAllItems(sortby, pageid, pagesize, null);
      }

      @GetMapping("/items/query/pages/{pagesize}/{pageid}")
      @ResponseBody
      public CommonResponse<IPage<ItemVO>> searchItems(@RequestParam String keyword, @RequestParam int categoryId,
                                                       @RequestParam int sortby, @PathVariable int pagesize,
                                                       @PathVariable int pageid, HttpSession httpSession) {
            //<categoryid>为0表示在所有商品中搜索
            User login_user = (User) httpSession.getAttribute("login_user");
            String username;
            if (login_user == null)
                  username = null;
            else
                  username = login_user.getUsername();
            return catalogMService.searchItemsPages(keyword, categoryId, sortby, pageid, pagesize, username);
      }

      @GetMapping("/items/{itemOrshopname}/{product}/pages/{pagesize}/{pageid}")
      @ResponseBody
      public CommonResponse<IPage<ItemVO>> searchItemsPages(@PathVariable String itemOrshopname, @PathVariable int product,
                                                            @RequestParam int sortby, @PathVariable int pagesize,
                                                            @PathVariable int pageid, HttpSession httpSession) {
            //<categoryid>为0表示在所有商品中搜索

            return catalogMService.searchItemsPagesByProduct(itemOrshopname, product, sortby, pageid, pagesize, null);
      }


      @PostMapping("/items")
      @ResponseBody
      public CommonResponse additem( @RequestBody  ItemVO2 itemVO2) {

            return catalogMService.additem(itemVO2);
      }


      @PutMapping("/items/{itemid}")
      @ResponseBody
      public CommonResponse updateitem(@PathVariable int itemid, @RequestBody Item item) {
            item.setId(itemid);
            item.setVideoid(0);
            return catalogMService.updateitem(item);
      }

      @DeleteMapping("/items/{itemid}")
      @ResponseBody
      public CommonResponse deleteitem(@PathVariable int itemid) {
            return catalogMService.deleteitem(itemid);
      }

      @PostMapping("/categories")
      @ResponseBody
      CommonResponse addCategory( @RequestBody Category category){
            return catalogMService.addCategory(category);
      }

      @PutMapping("/categories/{id}")
      @ResponseBody
      CommonResponse updateCategory(@PathVariable int id, @RequestBody Category category){
            category.setId(id);
            return catalogMService.updateCategory(category);
      }

      @PostMapping("/products")
      @ResponseBody
      CommonResponse addProduct( @RequestBody Product product){
            return catalogMService.addProduct(product);
      }

      @PutMapping("/products/{id}")
      @ResponseBody
      CommonResponse updateProduct(@PathVariable int id,  @RequestBody Product product){
            product.setId(id);
            return catalogMService.updateProduct(product);
      }

      @GetMapping("/attr")
      @ResponseBody
      CommonResponse<List<Attributes>> getAttributes(){
            return catalogMService.getAttributes();
      }

      @GetMapping("/attrvals/{attrid}")
      @ResponseBody
      CommonResponse<List<Attrvalues>> getAttrvalues(@PathVariable  int attrid){
            return catalogMService.getAttrvalues(attrid);
      }

}
